
var provincelist = [];
var citylist = [];
var districtlist = [];
var poundlist = [];

apiready = function(){

	do_ajax('/app/getprovince','get', {}, function(ret,err){
		api.hideProgress();

		if(ret.length > 0){
			provincelist = ret;
			showProvinceList(provincelist);
		}
		else{
			toast('暂无省份');
		}
	});
};

//省份列表
function showProvinceList(list){
	var aHtml = '';
	
	for(var i = 0; i < list.length; i++){

		aHtml += '<div class="am-u-sm-12 district"  onclick="selectProvince(\'' + i + '\');" style="">';
			aHtml += '<span style="font-weight:bold;">'+ list[i].name +'</span>';
		aHtml += '</div>';
	}
	
	$api.html($api.byId('list'), aHtml);
	$api.html($api.byId('title'), '选择省份');
}

//选择省
function selectProvince(index){
	var province = provincelist[index];
	
	do_ajax('/app/getcity?province='+province.name,'get', {}, function(ret,err){
		api.hideProgress();

		if(ret.length > 0){
			citylist = ret;
			showCityList(citylist);
		}
		else{
			toast('暂无城市');
		}
	});
}

//城市列表
function showCityList(list){
	var aHtml = '';
	
	for(var i = 0; i < list.length; i++){

		aHtml += '<div class="am-u-sm-12 district"  onclick="selectCity(\'' + i + '\');" style="">';
			aHtml += '<span style="font-weight:bold;">'+ list[i].name +'</span>';
		aHtml += '</div>';
	}
	
	$api.html($api.byId('list'), aHtml);
	$api.html($api.byId('title'), '选择城市');
}

//选择城市
function selectCity(index){
	var city = citylist[index];
	
	do_ajax('/app/getdistrict?city='+city.name,'get', {}, function(ret,err){
		api.hideProgress();

		if(ret.length > 0){
			districtlist = ret;
			showDistrictList(districtlist);
		}
		else{
			toast('暂无区');
		}
	});
}

//区列表
function showDistrictList(list){
	var aHtml = '';
	
	for(var i = 0; i < list.length; i++){

		aHtml += '<div class="am-u-sm-12 district"  onclick="selectDistrict(\'' + i + '\');" style="">';
			aHtml += '<span style="font-weight:bold;">'+ list[i].name +'</span>';
		aHtml += '</div>';
	}
	
	$api.html($api.byId('list'), aHtml);
	$api.html($api.byId('title'), '选择区');
}

//选择区
function selectDistrict(index){
	var district = districtlist[index];
	      
	do_ajax('/app/getpounds?county='+district.name,'get', {}, function(ret,err){
		api.hideProgress();

		if(ret.length > 0){
			poundlist = ret;
			showPoundList(poundlist);
		}
		else{
			toast('暂无小区');
		}
	});
}

//小区列表
function showPoundList(list){
	var aHtml = '';
	
	for(var i = 0; i < list.length; i++){

		aHtml += '<div class="am-u-sm-12 district"  onclick="selectPounds(\'' + i + '\');" style="">';
			aHtml += '<span style="font-weight:bold;">'+ list[i].name +'</span>';
		aHtml += '</div>';
	}
	
	$api.html($api.byId('list'), aHtml);
	$api.html($api.byId('title'), '选择小区');
}

function selectPounds(index){
	var pound = poundlist[index];
	
	//发送选择小区事件
	api.sendEvent({
	    name: 'selectLocation',
	    extra: {'city':pound.city,'county':pound.county,'pound':pound.name}                              
	});

	api.closeWin();
}